<html>
<head>	
	<script language="javascript">
		function validasi(form){
		  if ((form.judul.value == "") && (form.content.value == "")){
			alert("Data belum terisi.");
			form.judul.focus();
			return (false);
		  }
		  
		  if (form.judul.value == ""){
			alert("Anda belum mengisikan Judul.");
			form.judul.focus();
			return (false);
		  }
			 
		  if (form.content.value == ""){
			alert("Anda belum mengisikan content artikel.");
			form.content.focus();
			return (false);
		  }
		  return (true);
		}
	</script>
</head>
</html>
<?php
function GetCheckboxes($table, $key, $Label, $Nilai='') {
  $s = "select * from $table order by nama_tag";
  $r = mysql_query($s);
  $_arrNilai = explode(',', $Nilai);
  $str = '';
  while ($w = mysql_fetch_array($r)) {
    $_ck = (array_search($w[$key], $_arrNilai) === false)? '' : 'checked';
    $str .= "<input type=checkbox name='".$key."[]' value='$w[$key]' $_ck>$w[$Label] ";
  }
  return $str;
}

$aksi="modul/mod_berita/aksi_berita.php";
switch($_GET[act]){
  // Tampil Berita
  default:
    echo "<h2>Informasi Kesehatan</h2>
          <p><input type=button value='Tambah Artikel' onclick=\"window.location.href='?module=berita&act=tambahberita';\"></p>";?>
		  <div class="search">
				<form method="POST" action="?module=berita&act=cariberita">
					<fieldset>
						<input class="text" type="text" name="kata" onblur="if(this.value=='')this.value='pencarian artikel...';" onclick="if(this.value=='pencarian artikel...')this.value='';" name="search" value="pencarian artikel..." />
						<input class="submit" type="submit" value="" />
					</fieldset>
				</form>
			</div>
          <?php echo "<table id='rounded-corner'>
          <tr><th>No</th><th>Judul</th><th>Tanggal Posting</th><th>Waktu</th><th>Edit</th><th>Hapus</th></tr>";

    $p      = new Paging;
    $batas  = 10;
    $posisi = $p->cariPosisi($batas);

    if ($_SESSION[leveluser]=='admin'){
      $tampil = mysql_query("SELECT * FROM artikel ORDER BY id_content DESC LIMIT $posisi,$batas");
    }
    else{
      $tampil=mysql_query("SELECT * FROM artikel 
                           WHERE username='$_SESSION[namauser]'       
                           ORDER BY id_content DESC LIMIT $posisi,$batas");
    }
  
    $no = $posisi+1;
    while($r=mysql_fetch_array($tampil)){
      $tgl_posting=tgl_indo($r[tanggal]);
      echo "<tr><td>$no</td>
                <td>$r[judul]</td>
                <td>$tgl_posting</td>
				<td>$r[waktu]</td>
		            <td><a href=?module=berita&act=editberita&id=$r[id_content]><img border=0 title='' alt='' src='images/user_edit.png'></a></td> 
		            <td><a class='ask' href=$aksi?module=berita&act=hapus&id=$r[id_content]><img border=0 title='' alt='' src='images/trash.png'></a></td>
		        </tr>";
      $no++;
    }
    echo "</table>";

    if ($_SESSION[leveluser]=='admin'){
      $jmldata = mysql_num_rows(mysql_query("SELECT * FROM artikel"));
    }
    else{
      $jmldata = mysql_num_rows(mysql_query("SELECT * FROM artikel WHERE username='$_SESSION[namauser]'"));
    }  
    $jmlhalaman  = $p->jumlahHalaman($jmldata, $batas);
    $linkHalaman = $p->navHalaman($_GET[halaman], $jmlhalaman);

    echo "<div id=paging>Hal: $linkHalaman</div><br>";
 
    break;
  
  case "tambahberita":
    echo "<h2>Tambah Artikel</h2>
          <form method=POST action='$aksi?module=berita&act=input' enctype='multipart/form-data' onSubmit='return validasi(this)'>
          <table>
          <tr><td>Judul *)</td>     <td> : <input type=text name='judul' size=60></td></tr>
          <tr><td>Informasi *)</td><td> : <textarea name='content'  style='width: 550px; height: 300px;'></textarea></td></tr>";
         // <tr><td>Gambar</td>      <td> : <input type=file name='fupload' size=40> 
                                         // <br>Tipe gambar harus JPG/JPEG dan ukuran lebar maks: 400 px</td></tr>

    echo "</td></tr>
			<tr><td colspan=2>*) Harus diisi.</td></tr>
          <tr><td colspan=2><input type=submit value=Simpan>
                            <input type=button value=Batal onclick=self.history.back()></td></tr>
          </table></form>";
     break;
    
  case "editberita":
    $edit = mysql_query("SELECT * FROM artikel WHERE id_content='$_GET[id]'");
    $r    = mysql_fetch_array($edit);

    echo "<h2>Edit Informasi</h2>
          <form method=POST enctype='multipart/form-data' action=$aksi?module=berita&act=update>
          <input type=hidden name=id value=$r[id_content]>
          <table>
          <tr><td>Judul</td>     <td> : <input type=text name='judul' size=60 value='$r[judul]'></td></tr>
		  <tr><td>Informasi</td>   <td> :<textarea name='content' style='width: 500px; height: 300px;'>$r[content]</textarea></td></tr>";
//<tr><td>Gambar</td><td>  :   <img src='foto/foto_berita/$r[gambar]' style='width:260px; height: 200px;'></td></tr>
	//		</td></tr>
      //    <tr><td>Ganti Gbr</td>    <td> : <input type=file name='fupload' size=30> *)</td></tr>
        //  <tr><td colspan=2>*) Apabila gambar tidak diubah, dikosongkan saja.</td></tr>
 
    echo  "<tr><td colspan=2><input type=submit value=Update>
                            <input type=button value=Batal onclick=window.location='media.php?module=berita'></td></tr>
         </table></form>";
    break;  
	
	case "cariberita";
	$kata = trim($_POST[kata]);

	  // pisahkan kata per kalimat lalu hitung jumlah kata
	  $pisah_kata = explode(" ",$kata);
	  $jml_katakan = (integer)count($pisah_kata);
	  $jml_kata = $jml_katakan-1;

	  $cari = "SELECT * FROM artikel WHERE " ;
		for ($i=0; $i<=$jml_kata; $i++){
		  $cari .= "judul LIKE '%$pisah_kata[$i]%' OR content LIKE '%$pisah_kata[$i]%'";
		  if ($i < $jml_kata ){
			$cari .= " OR ";
		  }
		}
	  $hasil  = mysql_query($cari);
	  $ketemu = mysql_num_rows($hasil);
	  
		if ($ketemu > 0){
		echo "<p>Ditemukan <b>$ketemu</b> berita dengan kata <font style='background-color:#00FFFF'><b>$kata</b></font> : </p>"; 
		
		  
		  echo "<table id='rounded-corner'>
          <tr><th>No</th><th>Judul</th><th>Tanggal Posting</th><th>Waktu</th><th>Edit</th><th>Hapus</th></tr>";

			$p      = new Paging;
			$batas  = 10;
			$posisi = $p->cariPosisi($batas);

			if ($_SESSION[leveluser]=='admin'){
			  $tampil = mysql_query($cari." DESC LIMIT $posisi,$batas");
			}
			else{
			  $tampil=mysql_query($cari." AND WHERE username='$_SESSION[namauser]'       
								   ORDER BY id_content DESC LIMIT $posisi,$batas");
			}
			
			$no= $posisi+1;
			while($t=mysql_fetch_array($hasil)){
		  // Tampilkan hanya sebagian isi wisata
		  /*$namalengkap = htmlentities(strip_tags($t[namalengkap]));
		  $isi = substr($namalengkap,0,100);
		  $isi = substr($namalengkap,0,strrpos($isi," ")); */
				$tgl_posting=tgl_indo($t[tanggal]);
			  echo "<tr><td>$no</td>
					<td>$t[judul]</td>
					<td>$tgl_posting</td>
					<td>$t[waktu]</td>
						<td><a href=?module=berita&act=editberita&id=$t[id_content]><img border=0 title='' alt='' src='images/user_edit.png'></a></td> 
						<td><a class='ask' href=?module=berita&act=hapusberita&id=$t[id_content]><img border=0 title='' alt='' src='images/trash.png'></a></td>
					</tr>";
			$no++;
			}
			echo "</table>";
			
					
				 if ($_SESSION[leveluser]=='admin'){
					  $jmldata = mysql_num_rows(mysql_query($cari));
					}
					else{
					  $jmldata = mysql_num_rows(mysql_query($cari." WHERE username='$_SESSION[namauser]'"));
					}  
					$jmlhalaman  = $p->jumlahHalaman($jmldata, $batas);
					$linkHalaman = $p->navHalaman($_GET[halaman], $jmlhalaman);

					echo "<div id=paging>Hal: $linkHalaman</div><br>
					";
		}                                                          
	  else{
		echo "<p>Tidak ditemukan artikel dengan kata <b>$kata</b></p>";
	  }
	  echo "<input type=button value=Back onclick=window.location='media.php?module=berita'>";
	break;
	
}
?>
